package com.semicolon_dtn_student;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;

public class ScheduleMainActivity extends Activity {
	
	private ListView listView_schedule;
	private final static int ACT_REGISTER = 0;
	private DBHelper DB_helper;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.schedule_main);

        DB_helper = new DBHelper(this);
        
        Spinner spinner = (Spinner) findViewById(R.id.spinner_schedule_conditions); 

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
        		R.array.conditions_array, android.R.layout.simple_spinner_item); 

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
     
        spinner.setAdapter(adapter); 

        spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
        	public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
        	{
        		
        	}
        
			@Override
			public void onNothingSelected(AdapterView<?> arg0) 
			{
					
			}
		});
        
        
        ConstantVal.list_adapter = new ScheduleListAdapter(this, R.layout.schedule_list, ConstantVal.sched_list);
      	listView_schedule = (ListView) findViewById(R.id.list);

		DBtoArrayList();
      	listView_schedule.setAdapter(ConstantVal.list_adapter);
        
        Button button_main_schedule = (Button)findViewById(R.id.button_schedule_register);
        
        button_main_schedule.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {					
				
				Intent intent =  new Intent("android.intent.action.SCHEDULE_REGISTER");
				startActivityForResult(intent, ACT_REGISTER);
				
			}
       });
        
        listView_schedule.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        {
        	public void onItemClick(AdapterView<?> parent, View view, int position,long l_position) {
        		
        		Intent intent =  new Intent("android.intent.action.SCHEDULE_EDIT");
        		
        		intent.putExtra("list_pos", position);
        		
        		startActivity(intent);	
        	}
        });
        
         
    }
 
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
    	switch(requestCode)
    	{
    		case ACT_REGISTER:
    			if(resultCode == RESULT_OK)
    			{
    				ConstantVal.list_adapter.notifyDataSetChanged();
    			}
    		break;
    	}
    }

				
    public void DBtoArrayList()
    {
    	SQLiteDatabase db ;
    	db = DB_helper.getReadableDatabase();
    	Cursor mCursor = db.rawQuery("SELECT * FROM SCHEDULES ORDER BY " +
    			"day, month, hour1, minute1, hour2, minute2 DESC;", null);
    	int id_index = mCursor.getColumnIndex("_id");
    	int title_index = mCursor.getColumnIndex("title");
    	int location_index = mCursor.getColumnIndex("location");
    	int year_index = mCursor.getColumnIndex("year");
    	int month_index = mCursor.getColumnIndex("month");
    	int day_index = mCursor.getColumnIndex("day");
    	int hour1_index = mCursor.getColumnIndex("hour1");
    	int minute1_index = mCursor.getColumnIndex("minute1");
    	int hour2_index = mCursor.getColumnIndex("hour2");
    	int minute2_index = mCursor.getColumnIndex("minute2");
    	int memo_index = mCursor.getColumnIndex("memo");
    	int success_index = mCursor.getColumnIndex("success");

    	mCursor.moveToFirst();
    	

    	for(int i = ConstantVal.sched_list.size() - 1; i>=0  ; i--)
    	{
    		ConstantVal.sched_list.remove(i);
    	}
    	
    	while(!mCursor.isAfterLast())
    	{
    		int id = mCursor.getInt(id_index);
    		String title = mCursor.getString(title_index);
    		String location = mCursor.getString(location_index);
    		int year = mCursor.getInt(year_index);
    		int month = mCursor.getInt(month_index);
    		int day = mCursor.getInt(day_index);
    		int hour1 = mCursor.getInt(hour1_index);
    		int minute1 = mCursor.getInt(minute1_index);
    		int hour2 = mCursor.getInt(hour2_index);
    		int minute2 = mCursor.getInt(minute2_index);
    		String memo = mCursor.getString(memo_index);
    		int success = mCursor.getInt(success_index);
    		
    		ConstantVal.sched_list.add(new ScheduleInfo(title, location, year, month, day, hour1, minute1
    				,hour2, minute2, memo, success));

    		ConstantVal.sched_list.get(ConstantVal.sched_list.size() - 1).setId(id);
    		mCursor.moveToNext();
    	}

    	DB_helper.close();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
